import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../dd_models/dd_hive_works_model.dart';
import '../../../r.dart';
import '../controllers/dd_block_list_controller.dart';
import '../controllers/dd_my_atical_controller.dart';
import '../views/dd_view_background.dart';

class DDMyAticalPage extends StatefulWidget {
  const DDMyAticalPage({Key? key}) : super(key: key);

  @override
  State<DDMyAticalPage> createState() => _DDMyAticalPageState();
}

class _DDMyAticalPageState extends State<DDMyAticalPage> {
  DDMyAticalController _controller = DDMyAticalController();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DDMyAticalController>(
        init: _controller,
        initState: (state) {},
        dispose: (state) {},
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(
              backgroundColor: Color(0xFFE7F6FC),
              title: Text(
                'My Articles',
                style: TextStyle(fontSize: 18),
              ),
              leading: IconButton(
                icon: Image.asset(R.assetsImagesHomeSecond1),
                onPressed: () {
                  Get.back();
                },
              ),
            ),
            backgroundColor: Color(0xFFE7F6FC),
            body: Obx(
              () => _controller.datas.length == 0
                  ? SafeArea(
                      top: false,
                      child: Column(
                        children: [
                          SizedBox(
                            height: 148,
                          ),
                          Center(
                            child: Image.asset(R.assetsImagesPerson15),
                          )
                        ],
                      ))
                  : Stack(
                      children: [
                        Positioned.fill(
                          child: Image.asset(
                            R.assetsImagesPerson20,
                            fit: BoxFit.fill,
                          ),
                        ),
                        Positioned.fill(
                          child: SafeArea(
                            top: false,
                            child: Obx(
                              () => SingleChildScrollView(
                                child: Padding(
                                  padding: const EdgeInsets.all(16.0),
                                  child: _controller.datas.length == 0
                                      ? Column(
                                          children: [
                                            SizedBox(
                                              height: 148,
                                            ),
                                            Center(
                                              child: Image.asset(
                                                  R.assetsImagesPerson15),
                                            )
                                          ],
                                        )
                                      : GridView.builder(
                                          shrinkWrap: true,
                                          physics:
                                              NeverScrollableScrollPhysics(),
                                          padding: EdgeInsets.all(16),
                                          gridDelegate:
                                              SliverGridDelegateWithFixedCrossAxisCount(
                                            crossAxisCount: 2,
                                            crossAxisSpacing: 8,
                                            mainAxisSpacing: 20,
                                            childAspectRatio: 0.75,
                                          ),
                                          itemCount: controller.datas
                                              .length, // Replace with actual number of posts
                                          itemBuilder: (context, index) {
                                            HiveWorksModel model =
                                                controller.datas[index];
                                            return DDViewBackGround(
                                                content: Stack(
                                              children: [
                                                Column(
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                  children: [
                                                    Padding(
                                                      padding: EdgeInsets.only(
                                                          left: 4.0,
                                                          right: 4.0,
                                                          top: 4.0,
                                                          bottom: 0),
                                                      child: AspectRatio(
                                                        aspectRatio: 1.05,
                                                        child: ClipRRect(
                                                          borderRadius:
                                                              BorderRadius
                                                                  .vertical(
                                                            top:
                                                                Radius.circular(
                                                                    4),
                                                          ),
                                                          child:
                                                              model.coverImage !=
                                                                      null
                                                                  ? Image.asset(
                                                                      model.coverImage?[
                                                                              0] ??
                                                                          '',
                                                                      height:
                                                                          150,
                                                                      width: double
                                                                          .infinity,
                                                                      fit: BoxFit
                                                                          .cover,
                                                                    )
                                                                  : Image(
                                                                      image: Image.memory(
                                                                              model.allImageData![0])
                                                                          .image,
                                                                      fit: BoxFit
                                                                          .cover,
                                                                      height:
                                                                          150,
                                                                      width: double
                                                                          .infinity,
                                                                    ),
                                                        ),
                                                      ),
                                                    ),
                                                    Padding(
                                                      padding: EdgeInsets.only(
                                                          left: 8.0,
                                                          right: 8.0,
                                                          top: 8,
                                                          bottom: 0),
                                                      child: Text(
                                                        model.description ?? '',
                                                        style: TextStyle(
                                                            fontSize: 12),
                                                        maxLines: 1,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                      ),
                                                    ),
                                                    Padding(
                                                      padding: const EdgeInsets
                                                          .symmetric(
                                                          horizontal: 8.0),
                                                      child: Row(
                                                        children: [
                                                          CircleAvatar(
                                                            radius: 15,
                                                            backgroundImage:
                                                                NetworkImage(
                                                              model.avatarUrl ??
                                                                  '',
                                                            ),
                                                          ),
                                                          SizedBox(width: 8),
                                                          Expanded(
                                                            child: Text(
                                                              model.nickname ??
                                                                  '',
                                                              maxLines: 1,
                                                              style: TextStyle(
                                                                  fontSize: 14),
                                                              overflow:
                                                                  TextOverflow
                                                                      .ellipsis,
                                                            ),
                                                          ),
                                                          Container(
                                                            width: 40,
                                                            height: 40,
                                                            child: IconButton(
                                                              icon: Image.asset(
                                                                  (model.myLikes ??
                                                                          false)
                                                                      ? R.assetsImagesHomeSecond6
                                                                      : R.assetsImagesHomeSecond4),
                                                              onPressed: () {
                                                                controller
                                                                    .updateMyLike(
                                                                        model);
                                                              },
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                                Positioned(
                                                  right: -2,
                                                  top: -2,
                                                  child: Container(
                                                    child: IconButton(
                                                      icon: Image.asset(
                                                        R.assetsImagesPublish5,
                                                        fit: BoxFit.cover,
                                                        height: 24,
                                                        width: 24,
                                                      ),
                                                      onPressed: () {
                                                        _controller
                                                            .removeMyArticles(
                                                                model);
                                                      },
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ));
                                          },
                                        ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
            ),
          );
        });
  }
}
